{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 矩阵加减、数乘\n",
    "《线性代数，初阶》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "该代码主要演示了矩阵的基本运算，包括矩阵的加法、减法、数乘以及零矩阵和单位矩阵的构造。我们从数学的角度详细描述每个步骤，并用数学公式表示。\n",
    "\n",
    "---\n",
    "\n",
    "### **1. 初始化**\n",
    "代码首先导入 `numpy` 作为数学计算库，以便处理矩阵运算。\n",
    "\n",
    "---\n",
    "\n",
    "### **2. 矩阵 $A$ 和 $B$ 的定义**\n",
    "代码使用 `np.random.randint(0, 10, (5,8))` 生成两个 $5 \\times 8$ 的随机整数矩阵 $A$ 和 $B$，其元素范围在 $[0,10)$ 之间。\n",
    "\n",
    "设 $A$ 和 $B$ 为：\n",
    "$$\n",
    "A = \\begin{bmatrix}\n",
    "a_{1,1} & a_{1,2} & \\dots & a_{1,8} \\\\\n",
    "a_{2,1} & a_{2,2} & \\dots & a_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "a_{5,1} & a_{5,2} & \\dots & a_{5,8}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "$$\n",
    "B = \\begin{bmatrix}\n",
    "b_{1,1} & b_{1,2} & \\dots & b_{1,8} \\\\\n",
    "b_{2,1} & b_{2,2} & \\dots & b_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "b_{5,1} & b_{5,2} & \\dots & b_{5,8}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "其中 $a_{i,j}, b_{i,j} \\in \\{0,1,2,\\dots,9\\}$。\n",
    "\n",
    "---\n",
    "\n",
    "### **3. 矩阵加法**\n",
    "代码计算 $A$ 和 $B$ 的矩阵加法：\n",
    "$$\n",
    "C = A + B\n",
    "$$\n",
    "即\n",
    "$$\n",
    "C = \\begin{bmatrix}\n",
    "a_{1,1} + b_{1,1} & a_{1,2} + b_{1,2} & \\dots & a_{1,8} + b_{1,8} \\\\\n",
    "a_{2,1} + b_{2,1} & a_{2,2} + b_{2,2} & \\dots & a_{2,8} + b_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "a_{5,1} + b_{5,1} & a_{5,2} + b_{5,2} & \\dots & a_{5,8} + b_{5,8}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "矩阵加法要求 $A$ 和 $B$ 形状相同，按 **逐元素** 相加。\n",
    "\n",
    "---\n",
    "\n",
    "### **4. 矩阵减法**\n",
    "代码计算 $A$ 和 $B$ 的矩阵减法：\n",
    "$$\n",
    "D = A - B\n",
    "$$\n",
    "即\n",
    "$$\n",
    "D = \\begin{bmatrix}\n",
    "a_{1,1} - b_{1,1} & a_{1,2} - b_{1,2} & \\dots & a_{1,8} - b_{1,8} \\\\\n",
    "a_{2,1} - b_{2,1} & a_{2,2} - b_{2,2} & \\dots & a_{2,8} - b_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "a_{5,1} - b_{5,1} & a_{5,2} - b_{5,2} & \\dots & a_{5,8} - b_{5,8}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "矩阵减法与加法类似，按 **逐元素** 相减。\n",
    "\n",
    "---\n",
    "\n",
    "### **5. 矩阵的数乘**\n",
    "代码计算 $A$ 的数乘：\n",
    "$$\n",
    "k \\cdot A\n",
    "$$\n",
    "其中 $k = 2$，即：\n",
    "$$\n",
    "2 \\times \\begin{bmatrix}\n",
    "a_{1,1} & a_{1,2} & \\dots & a_{1,8} \\\\\n",
    "a_{2,1} & a_{2,2} & \\dots & a_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "a_{5,1} & a_{5,2} & \\dots & a_{5,8}\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "2a_{1,1} & 2a_{1,2} & \\dots & 2a_{1,8} \\\\\n",
    "2a_{2,1} & 2a_{2,2} & \\dots & 2a_{2,8} \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "2a_{5,1} & 2a_{5,2} & \\dots & 2a_{5,8}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "标量数乘就是将 $A$ 的每个元素都乘以 $2$。\n",
    "\n",
    "此外，代码计算：\n",
    "$$\n",
    "0 \\times A\n",
    "$$\n",
    "结果是一个 **全零矩阵**：\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "0 & 0 & \\dots & 0 \\\\\n",
    "0 & 0 & \\dots & 0 \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "0 & 0 & \\dots & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **6. 构造零矩阵**\n",
    "`np.zeros_like(A)` 生成与 $A$ 形状相同的全零矩阵：\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "0 & 0 & \\dots & 0 \\\\\n",
    "0 & 0 & \\dots & 0 \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "0 & 0 & \\dots & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **7. 构造全 1 矩阵**\n",
    "`np.ones_like(A)` 生成与 $A$ 形状相同的全 1 矩阵：\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 1 & \\dots & 1 \\\\\n",
    "1 & 1 & \\dots & 1 \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "1 & 1 & \\dots & 1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "这种矩阵在计算中通常用于 **元素加法的单位矩阵**，即：\n",
    "$$\n",
    "A + O = \\begin{bmatrix}\n",
    "a_{1,1} + 1 & a_{1,2} + 1 & \\dots & a_{1,8} + 1 \\\\\n",
    "a_{2,1} + 1 & a_{2,2} + 1 & \\dots & a_{2,8} + 1 \\\\\n",
    "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "a_{5,1} + 1 & a_{5,2} + 1 & \\dots & a_{5,8} + 1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "1. **矩阵加法** 和 **矩阵减法** 都是 **逐元素** 计算，要求形状相同。\n",
    "2. **数乘** 将矩阵的每个元素都乘以标量。\n",
    "3. **零矩阵** 是所有元素为零的矩阵，通常用于初始化或作为加法的单位元。\n",
    "4. **全 1 矩阵** 可以用于对矩阵所有元素进行平移（加 1）。\n",
    "\n",
    "通过这些操作，可以构建更复杂的矩阵计算！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f09495f-f025-46fe-94f4-e1244f86b299",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "19c2dad5-782b-4b6b-a451-da4c7b1a2319",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03aafc3d-2d61-47b0-8c6d-c075957d579f",
   "metadata": {},
   "source": [
    "## 定义两个相同大小的矩阵 A 和 B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8c0f5069-59e8-487c-b0cc-29c4cf97faf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(88)  # 保证每次运行生成相同的随机数\n",
    "A = np.random.randint(0, 10, (5, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1bfba2b-1881-47b3-93b7-4bc43846273e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 0, 1, 4, 5, 1, 0, 5],\n",
       "       [0, 2, 1, 4, 7, 9, 7, 7],\n",
       "       [6, 6, 3, 1, 0, 8, 6, 0],\n",
       "       [0, 5, 7, 5, 3, 9, 5, 3],\n",
       "       [7, 3, 9, 3, 8, 5, 0, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "828bb526-1d59-419f-ac19-924de8517ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.random.randint(0, 10, (5, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8eb57079-b765-4b16-99f3-e52f0c639414",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 2, 2, 5, 9, 2, 5],\n",
       "       [6, 6, 1, 1, 8, 6, 1, 2],\n",
       "       [1, 7, 4, 3, 6, 6, 6, 0],\n",
       "       [3, 0, 8, 2, 7, 0, 9, 7],\n",
       "       [2, 6, 3, 6, 8, 6, 4, 3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f366f78-7c5f-49eb-8da3-72dc47a5742f",
   "metadata": {},
   "source": [
    "## 矩阵加法：A + B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d655b3a5-5069-484e-8f1b-431e4efe1765",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12,  0,  3,  6, 10, 10,  2, 10],\n",
       "       [ 6,  8,  2,  5, 15, 15,  8,  9],\n",
       "       [ 7, 13,  7,  4,  6, 14, 12,  0],\n",
       "       [ 3,  5, 15,  7, 10,  9, 14, 10],\n",
       "       [ 9,  9, 12,  9, 16, 11,  4,  9]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_addition = A + B\n",
    "matrix_addition"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6e7a4d9-8c20-475b-8ce9-ea6fc9753814",
   "metadata": {},
   "source": [
    "## 矩阵减法：A - B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bd9d9333-89ee-4806-b682-37ab254b2fd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  0, -1,  2,  0, -8, -2,  0],\n",
       "       [-6, -4,  0,  3, -1,  3,  6,  5],\n",
       "       [ 5, -1, -1, -2, -6,  2,  0,  0],\n",
       "       [-3,  5, -1,  3, -4,  9, -4, -4],\n",
       "       [ 5, -3,  6, -3,  0, -1, -4,  3]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_subtraction = A - B\n",
    "matrix_subtraction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe270cae-7de8-4b62-8142-7dbeb4be660b",
   "metadata": {},
   "source": [
    "## 矩阵的数乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6c13da6e-1914-451a-8593-4e1ff707735a",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 2 # 标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bd2983e4-ef5a-42b2-803c-e7065649260d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16,  0,  2,  8, 10,  2,  0, 10],\n",
       "       [ 0,  4,  2,  8, 14, 18, 14, 14],\n",
       "       [12, 12,  6,  2,  0, 16, 12,  0],\n",
       "       [ 0, 10, 14, 10,  6, 18, 10,  6],\n",
       "       [14,  6, 18,  6, 16, 10,  0, 12]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_scalar_multiplication = k * A\n",
    "matrix_scalar_multiplication"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 * A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2bcebf81-f1b9-45fa-a93e-8de7bfcfa318",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros_like(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "18eaeb06-71d9-4e99-9ca7-e3ff2b0f6054",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a39ec1c1-ea16-48c0-a52a-b7f3209ad18b",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
